import moment from 'moment'

export const getUrlKey = (name) => {
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  let r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  } else {
    return null;
  }
}
export const getSounds = (time) => {
  if (time) {
    return Math.floor(time.valueOf() / 1000)
  }
}

export const limitWords = (val, num = 30) => {
  if (val && val.length >= num) {
    return `${val.substring(0, num)}...`
  }
  return val
}

export const formatTime = (data, type) => {
  const format = type || 'MM-DD'
  return moment(data).format(format)
}

export const diffTime = (data) => {
  const now = moment()
  const bef = moment(data)
  return moment(now - bef).format('D天')
}
